/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controller;

import java.util.List;
import javax.naming.InitialContext;
import javax.naming.NamingException;  
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;
import org.hibernate.classic.Session;




/**
 *
 * @author iron
 */
public class DAOFactory {  
  
   private SessionFactory factory;  
   private Transaction tx;  
   private Configuration cfg;  
   private Session session;  
   private InitialContext ic;  
  
   //Construtor   
   public DAOFactory() throws NamingException, HibernateException {  
  
      ic = new InitialContext();  
      factory = buildSessionFactory();      
      session = factory.openSession();  
      beginTransaction(); 
   }  
  
    private static SessionFactory buildSessionFactory(){
        try {
            // Create the SessionFactory from hibernate.cfg.xml
            return new AnnotationConfiguration().configure().buildSessionFactory();
 
        }
        catch (Throwable ex) {
            // Make sure you log the exception, as it might be swallowed
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }
   
   //commit no banco  
   public void commit() throws HibernateException {  
  
      try {  
         tx.commit();  
      } catch (HibernateException he) {  
         System.out.println(  
            "Generic Persistence -> Erro no commit:" + he.getMessage());  
         throw he;  
      }  
   }  
  
   //rollback no banco  
   public void rollback() throws HibernateException {  
  
      try {  
         tx.rollback();  
  
      } catch (HibernateException he) {  
         System.out.println(  
            "Generic Persistence -> Erro no rollback:" + he.getMessage());  
         throw he;  
      }  
   }  
  
   //inicia a transaction  
   private void beginTransaction() throws HibernateException {  
  
      try {  
         tx = session.beginTransaction();  
      } catch (HibernateException he) {  
         System.out.println(  
            "Generic Persistence -> Erro na transaction:"  
               + he.getMessage());  
         throw he;  
      }  
   }  
  
   //fecha a transacao  
   public void closeSession() throws HibernateException {  
  
      try {  
         session.close();  
      } catch (HibernateException he) {  
         System.out.println(  
            "Generic Persistence -> Erro no close:" + he.getMessage());  
         throw he;  
      }  
  
   }  
  
   //Insert  
   public void insert(Object objeto) throws Exception {  
  
      try {  
  
         session.save(objeto);  
         session.flush();  
  
      } catch (HibernateException he) {  
         System.out.println(  
            "Generic Persistence -> Erro na inserção :" + he.getMessage());  
         throw he;  
      }  
   }  
  
   //getList  
   public java.util.List getList(String query) throws Exception {  
  
      Query q1 = session.createQuery(query);  
      List objeto = q1.list();  
  
      return objeto;  
  
   }  
  
   //Retrieve  
   public Object retrieve(Integer pk, Class classe) throws Exception {  
  
      Object objeto = null;  
  
      try {  
         objeto = (Object) session.load(classe, pk);  
  
      } catch (Exception e) {  
         e.printStackTrace();  
      } finally {  
         return objeto;  
      }  
   }  
  
   //Update  
   public void update(Object objeto, Integer pk) throws Exception {  
  
      try {  
  
         session.update(objeto, pk);  
         session.flush();  
  
      } catch (HibernateException he) {  
         System.out.println(  
            "Generic Persistence -> Erro na alteração: " + he.getMessage());  
         throw he;  
      }  
   }  
  
   //Delete  
   public void delete(Object objeto) throws Exception {  
  
      try {  
  
         session.delete(objeto);  
         session.flush();  
  
      } catch (HibernateException he) {  
         System.out.println(  
            "Generic Persistence -> Erro na exclusão: " + he.getMessage());  
         throw he;  
      }  
   }  
}



   
    
 